﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Foundation.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Foundation.Component
{
    public class AreaComponet : ComponentBase
    {


        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <returns>是否添加成功</returns>
        public bool Add(AreaEntity Details)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Area_Add", Details) > 0);
        }

        /// <summary>
        /// 查询所有地区的记录
        /// </summary>
        /// <returns>基础资料地区集合</returns>
        public List<AreaEntity> GetAreaList()
        {
            return FishBLLHelper.CallSpGetDataItemList<AreaEntity>("sgrf_foundation_Area_GetAll", null);
        }

        /// <summary>
        /// 根据Keyid获取一位用户
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>一位用户</returns>
        public AreaEntity GetAreaByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<AreaEntity>("sgrf_foundation_Area_GetAreaByKeyid", null, keyid);
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="UserDetails">一条记录</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateArea(AreaEntity area)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Area_Update", area) > 0);
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteArea(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Area_Delete", null, keyid) > 0);
        }

        /// <summary>
        /// 删除一条记录相关的parentkeyid
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteAreaAndParentKeyid(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_Area_DeleteAndDelParent", null, keyid) > 0);
        }
        /// <summary>
        /// 进行过滤、排序、分页查询基础资料类别
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>类别集合</returns>
        public List<AreaEntity> PagingGetArea(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "AreaCode DESC";
            }

            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AreaCode", DbType.String, " AND AreaCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AreaName", DbType.String, " AND AreaName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AdminAreaName", DbType.String, " AND AdminAreaName like '%{0}%' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<AreaEntity>("sgrf_foundation_Area_PagingGetAreas", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 根据过滤条件查询
        /// </summary>
        /// <param name="filterConditions">条件值</param>
        /// <returns>查询的集合</returns>
        public List<AreaEntity> DoSelectByArea(string filterConditions)
        {
            NameValueCollection fieldConllection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "AreaCode", DbType.String, "AND AreaCode like '%{0}%'"));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "AreaName", DbType.String, " AND AreaName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldConllection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<AreaEntity>("sgrf_foundation_Area_DoSelectByConditions",null,filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "areacode":
                    case "areaname":
                    case "issys":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤，排序、分页查询基础资料类别记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int GetAreaCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AreaCode", DbType.String, " AND AreaCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AreaName", DbType.String, " AND AreaName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "AdminAreaName", DbType.String, " AND AdminAreaName like '%{0}%' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_foundation_Area_PagingGetAreaCount", null, filterConditions));
        }
    }
}
